/**
 * Project: tourerp-core-java
 *
 * File Created at 2012-8-2
 * $Id$
 *
 * Copyright 2009 Hangzhou Unibon Technology Inc., All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Unibon Technology Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with tourerp.cn .
 */
package com.coin900.coin.base.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Locale;
import java.util.Map;

/**
 * TODO Comment of FreemarkerUtils
 *
 * @author libo
 */
public class FreemarkerUtils {
    private static final Log logger = LogFactory.getLog(FreemarkerUtils.class);

    /**
     * 处理模板
     *
     * @param tmpPath
     * @param tmpName
     * @param outPath
     * @param params
     * @throws IOException
     */
    public static void process(String tmpPath, String tmpName, String outPath, Object params) throws IOException {
        File file = new File(outPath);
        Configuration cfg = new Configuration();
        cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
        try {
            //设置模板路径
            cfg.setDirectoryForTemplateLoading(new File(tmpPath));
            cfg.setEncoding(Locale.CHINA, "UTF-8");
            Template t = cfg.getTemplate(tmpName);
            t.setEncoding("UTF-8");
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
            t.process(params, oWriter);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } catch (TemplateException e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }
}
